//
//  SISSContractFillInPlatTypeHouseCell.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/9/22.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSContractFillInPlatTypeHouseCell.h"

@interface SISSContractFillInPlatTypeHouseCell ()<UITextFieldDelegate>

/// roomTextField
@property (weak, nonatomic) UITextField *roomTextField;
/// roomUnitLabel
@property (weak, nonatomic) UILabel *roomUnitLabel;
/// hallTextField
@property (weak, nonatomic) UITextField *hallTextField;
/// hallUnitLabel
@property (weak, nonatomic) UILabel *hallUnitLabel;
/// toiletTextField
@property (weak, nonatomic) UITextField *toiletTextField;
/// toiletUnitLabel
@property (weak, nonatomic) UILabel *toiletUnitLabel;
/// balconyTextField
@property (weak, nonatomic) UITextField *balconyTextField;
/// balconyUnitLabel
@property (weak, nonatomic) UILabel *balconyUnitLabel;
/* bottomLine */
@property (weak, nonatomic) UIView *bottomLine;


/*  */
@property (strong, nonatomic) NSMutableArray *arrM;

@end
@implementation SISSContractFillInPlatTypeHouseCell

- (void)setContractModel:(SISSContractModel *)contractModel {
    [super setContractModel:contractModel];
    
    self.roomTextField.placeholder = @"几";
    self.hallTextField.placeholder = @"几";
    self.toiletTextField.placeholder = @"几";
    self.balconyTextField.placeholder = @"几";
    
    self.roomTextField.text = contractModel.array[0] ? [NSString stringWithFormat:@"%@",contractModel.array[0]] : @"";
    self.hallTextField.text = contractModel.array[1] ? [NSString stringWithFormat:@"%@",contractModel.array[1]] : @"";
    self.toiletTextField.text = contractModel.array[2] ? [NSString stringWithFormat:@"%@",contractModel.array[2]] : @"";
    self.balconyTextField.text = contractModel.array[3] ? [NSString stringWithFormat:@"%@",contractModel.array[3]] : @"";
    
}


#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if (textField == self.roomTextField) {
        [self.arrM replaceObjectAtIndex:0 withObject:self.roomTextField.text];
    } else if (textField == self.hallTextField) {
        [self.arrM replaceObjectAtIndex:1 withObject:self.hallTextField.text];
    } else if (textField == self.toiletTextField) {
        [self.arrM replaceObjectAtIndex:2 withObject:self.toiletTextField.text];
    } else if (textField == self.balconyTextField) {
        [self.arrM replaceObjectAtIndex:3 withObject:self.balconyTextField.text];
    }
    
    self.contractModel.array = self.arrM.copy;
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField == self.balconyTextField) {
        NSString *str = [NSString stringWithFormat:@"%@%@",textField.text,string];
        if (str.length > 2) {
            [SISSTools showSVTitle:@"不超过2位数字" andTime:1 andColor:YES];
            return NO;
        }
    }
    return YES;
}

#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UITextField *roomTextField = [[UITextField alloc] init];
    roomTextField.textColor = MAIN_TEXT_COLOR;
    roomTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:SECONDARY_TEXT_COLOR}];
    roomTextField.placeholder = @"几";
    roomTextField.textAlignment = NSTextAlignmentCenter;
    roomTextField.keyboardType = UIKeyboardTypeNumberPad;
    roomTextField.delegate = self;
    [self.contentView addSubview:roomTextField];
    self.roomTextField = roomTextField;
    
    UILabel *roomUnitLabel = [[UILabel alloc] init];
    roomUnitLabel.textColor = MAIN_TEXT_COLOR;
    roomUnitLabel.font = [UIFont systemFontOfSize:16];
    roomUnitLabel.text = @"房";
    [self.contentView addSubview:roomUnitLabel];
    self.roomUnitLabel = roomUnitLabel;
    
    UITextField *hallTextField = [[UITextField alloc] init];
    hallTextField.textColor = MAIN_TEXT_COLOR;
    hallTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:SECONDARY_TEXT_COLOR}];
    hallTextField.placeholder = @"几";
    hallTextField.delegate = self;
    hallTextField.textAlignment = NSTextAlignmentCenter;
    hallTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.contentView addSubview:hallTextField];
    self.hallTextField = hallTextField;
    
    UILabel *hallUnitLabel = [[UILabel alloc] init];
    hallUnitLabel.textColor = MAIN_TEXT_COLOR;
    hallUnitLabel.font = [UIFont systemFontOfSize:16];
    hallUnitLabel.textAlignment = NSTextAlignmentRight;
    hallUnitLabel.text = @"厅";
    [self.contentView addSubview:hallUnitLabel];
    self.hallUnitLabel = hallUnitLabel;
    
    UITextField *toiletTextField = [[UITextField alloc] init];
    toiletTextField.textColor = MAIN_TEXT_COLOR;
    toiletTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:SECONDARY_TEXT_COLOR}];
    toiletTextField.placeholder = @"几";
    toiletTextField.textAlignment = NSTextAlignmentCenter;
    toiletTextField.keyboardType = UIKeyboardTypeNumberPad;
    toiletTextField.delegate = self;
    [self.contentView addSubview:toiletTextField];
    self.toiletTextField = toiletTextField;
    
    UILabel *toiletUnitLabel = [[UILabel alloc] init];
    toiletUnitLabel.textColor = MAIN_TEXT_COLOR;
    toiletUnitLabel.font = [UIFont systemFontOfSize:16];
    toiletUnitLabel.text = @"卫";
    [self.contentView addSubview:toiletUnitLabel];
    self.toiletUnitLabel = toiletUnitLabel;
    
    UITextField *balconyTextField = [[UITextField alloc] init];
    balconyTextField.textColor = MAIN_TEXT_COLOR;
    balconyTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:SECONDARY_TEXT_COLOR}];
    balconyTextField.placeholder = @"几";
    balconyTextField.delegate = self;
    balconyTextField.textAlignment = NSTextAlignmentCenter;
    balconyTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.contentView addSubview:balconyTextField];
    self.balconyTextField = balconyTextField;
    
    UILabel *balconyUnitLabel = [[UILabel alloc] init];
    balconyUnitLabel.textColor = MAIN_TEXT_COLOR;
    balconyUnitLabel.font = [UIFont systemFontOfSize:16];
    balconyUnitLabel.textAlignment = NSTextAlignmentRight;
    balconyUnitLabel.text = @"阳台";
    [self.contentView addSubview:balconyUnitLabel];
    self.balconyUnitLabel = balconyUnitLabel;
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:bottomLine];
    self.bottomLine = bottomLine;
    
    
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    
    [self.roomTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.roomUnitLabel.mas_left).offset(-margin);
        make.centerY.equalTo(weakSelf.contentView);
        make.width.mas_equalTo(4 * margin);
    }];
    
    [self.roomUnitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.hallTextField.mas_left).offset(-margin);
        make.centerY.equalTo(weakSelf.contentView);
    }];
    
    [self.hallTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.hallUnitLabel.mas_left).offset(-margin);
        make.centerY.equalTo(weakSelf.contentView);
        make.width.mas_equalTo(4 * margin);
    }];
    
    [self.hallUnitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.toiletTextField.mas_left).offset(-margin);
        make.centerY.equalTo(weakSelf.contentView);
    }];
    
    [self.toiletTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.toiletUnitLabel.mas_left).offset(-margin);
        make.centerY.equalTo(weakSelf.contentView);
        make.width.mas_equalTo(4 * margin);
    }];
    
    [self.toiletUnitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.balconyTextField.mas_left).offset(-margin);
        make.centerY.equalTo(weakSelf.contentView);
    }];
    
    [self.balconyTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.balconyUnitLabel.mas_left).offset(-margin);
        make.centerY.equalTo(weakSelf.balconyUnitLabel);
        make.width.mas_equalTo(4 * margin);
    }];
    
    [self.balconyUnitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(1.5 * margin);
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.balconyUnitLabel.mas_bottom).offset(1.5 * margin);
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
}


- (NSMutableArray *)arrM {
    if (!_arrM) {
        _arrM = [NSMutableArray arrayWithCapacity:4];
        for (NSInteger i = 0; i < 4; i++) {
            [_arrM addObject:@""];
        }
    }
    return _arrM;
}

@end
